#!/usr/bin/env python
# Usage:
#   shard.py FILE X
# Reads in lines from FILE and splits them into X shards. The shards will be
# output as files of the form FILE_shardY, where 0 <= Y < X.

import sys

def main():
  input_file = open(sys.argv[1], 'r')
  ways = int(sys.argv[2])
  output_files = [
      open(sys.argv[1] + '_shard' + str(i), 'w') for i in range(0, ways)]
  i = 0
  for line in input_file:
    output_files[i].write(line)
    i += 1
    i %= ways

if __name__ == '__main__':
  main()
